package com.bocai.kelala.ui.travel.set.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelala.App;
import com.bocai.kelala.R;
import com.bocai.kelala.base.BaseActivity;
import com.bocai.kelala.bean.MyInvoiceBean;
import com.bocai.kelala.bean.MyInvoiceDetailBean;
import com.bocai.kelala.ui.travel.main.act.MainActivity;
import com.bocai.kelala.ui.travel.set.ada.MyInvoiceAda;
import com.bocai.kelala.ui.travel.set.contract.MyInvoiceContract;
import com.bocai.kelala.ui.travel.set.module.MyInvoiceModule;
import com.bocai.kelala.ui.travel.set.presenter.MyInvoicePresenter;
import com.bocai.kelala.ui.travel.stroke.presenter.MyStrokePresenter;
import com.bocai.mybase.base.BaseRecyclerAdapter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yuanfei on 2017/6/9.
 * 我的发票
 */

public class MyInvoiceAct extends BaseActivity implements MyInvoiceContract.view,XRecyclerView.LoadingListener {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.xrecycle)
    XRecyclerView xrecycle;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    MyInvoiceAda ada;
    @Inject
    MyInvoicePresenter presenter;
    private int pageIndex = 1;

    @Override
    public int getLayoutId() {
        return R.layout.act_my_invoice;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new MyInvoiceModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("我的发票");
        initBack(imgBack,llBack);
        ada = new MyInvoiceAda();
        xrecycle.setLayoutManager(new LinearLayoutManager(this));
        ada = new MyInvoiceAda();
        xrecycle.setAdapter(ada);
        ada.setOnItemOnListener(new BaseRecyclerAdapter.OnItemClickedListener() {
            @Override
            public void onItemClicked(int id, int index) {
                if (ada.getDataList().get(index).getBillingStatus() == 0) {
                    startActivity(new Intent(MyInvoiceAct.this,MyAddInvoice.class).putExtra("orderId",ada.getDataList().get(index).getOrderId()));
                }else {
                    startActivity(new Intent(MyInvoiceAct.this,MyInvoiceStatusActivity.class).putExtra("orderId",ada.getDataList().get(index).getOrderId()));
                }
            }
        });
        xrecycle.setLoadingListener(this);
        xrecycle.refresh();

    }

    @Override
    public void getMyInvoiceList(MyInvoiceBean bean) {
        xrecycle.refreshComplete();
        xrecycle.loadMoreComplete();
        if (bean !=null&&bean.getOrderList()!=null){
            if (pageIndex == 1){
                ada.clear();
                ada.addItems(bean.getOrderList());
            }else{
                ada.addItems(bean.getOrderList());
            }
            if (bean.getOrderList().size()>10){
                xrecycle.setLoadingMoreEnabled(false);
            }else {
                xrecycle.setLoadingMoreEnabled(true);
            }
        }
    }

    @Override
    public void openInvoiceSuccess() {

    }

    @Override
    public void getInvoiceDetail(MyInvoiceDetailBean bean) {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {

        if (msg.equals("用户授权失败")){
            msg = "您的登录已失效，请重新登录";
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
        showShortToast(msg);
    }

    @Override
    public void onRefresh() {
        pageIndex = 1;
        presenter.myInvoice(pageIndex+"","10");
    }

    @Override
    public void onLoadMore() {
        pageIndex = pageIndex + 1;
        presenter.myInvoice(pageIndex+"","10");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 444){
            xrecycle.refresh();
        }
    }
}
